package com.zhy.zhy_ormlite.db;

import java.sql.SQLException;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.zhy.zhy_ormlite.bean.Article;
import com.zhy.zhy_ormlite.bean.User;

public class ArticleDao {
    private Dao<Article, Integer> articleDaoOpe;
    private DatabaseHelper helper;

    @SuppressWarnings("unchecked")
    public ArticleDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
            articleDaoOpe = helper.getDao(Article.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一个Article
     *
     * @param article
     */
    public void add(Article article) {
        try {
            articleDaoOpe.create(article);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除
     */
    public void delete() {
        try {
            articleDaoOpe.updateRaw("delete from tb_article where id=2;");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 修改一个Article
     */
    public void update() {
        try {
//            articleDaoOpe.updateBuilder().updateColumnValue("title", "池锦标修改过的").where().eq("id", 1);
            articleDaoOpe.updateRaw("update tb_article set title='池锦标修改过的' where id=2;");

        } catch (SQLException e) {
            e.printStackTrace();
            Log.i("update", "修改失败");
        }
    }


    /**
     * 查询所有
     */
    public void queryAll() {
        try {
//            articleDaoOpe.updateBuilder().updateColumnValue("title", "池锦标修改过的").where().eq("id", 1);
            articleDaoOpe.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            Log.i("queryAll", "查询失败");
        }
    }


    /**
     * 通过Id得到一个Article
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public Article getArticleWithUser(int id) {
        Article article = null;
        try {
            article = articleDaoOpe.queryForId(id);
            helper.getDao(User.class).refresh(article.getUser());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return article;
    }

    /**
     * 通过Id得到一篇文章
     *
     * @param id
     * @return
     */
    public Article get(int id) {
        Article article = null;
        try {
            article = articleDaoOpe.queryForId(id);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return article;
    }

    public List<Article> allArticle() {
        List<Article> list = null;
        try {
            list = articleDaoOpe.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return list;

    }

    public void delArticle(Article article) {
        try {
            articleDaoOpe.delete(article);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 通过UserId获取所有的文章
     *
     * @param userId
     * @return
     */
    public List<Article> listByUserId(int userId) {
        try {
            /*QueryBuilder<Article, Integer> articleBuilder = articleDaoOpe
                    .queryBuilder();
			QueryBuilder userBuilder = helper.getDao(User.class).queryBuilder();
			articleBuilder.join(userBuilder);
			
			
			Where<Article, Integer> where = queryBuilder.where();
			where.eq("user_id", 1);
			where.and();
			where.eq("name", "xxx");

			// 或者
			articleDaoOpe.queryBuilder().//
					where().//
					eq("user_id", 1).and().//
					eq("name", "xxx");
			//
			articleDaoOpe.updateBuilder().updateColumnValue("name","zzz").where().eq("user_id", 1);
			where.or(
					//
					where.and(//
							where.eq("user_id", 1), where.eq("name", "xxx")),
					where.and(//
							where.eq("user_id", 2), where.eq("name", "yyy")));*/

            return articleDaoOpe.queryBuilder().where().eq("user_id", userId)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
